/*
 * Copyright (C) 2009 Kristopher T Babic
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, see <http://www.gnu.org/licenses>.
 */

package edu.idp.client;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;

/**
 *
 * @author Kristopher T Babic
 */
public class ImageSelection extends JFrame implements ActionListener, MouseListener, ListSelectionListener, DocumentListener {

    private JList imageList = null;
    private JButton openButton = null;
    private JButton cancelButton = null;
    private JScrollPane scrollPane = null;
    private JLabel imageLabel = null;
    private JTextField imageText = null;
    private Vector actionListeners = null;

    private String selectedImage = null;

    private boolean solarisWorkaround = false;

    public ImageSelection(String data[]) {

//    JPanel p = new JPanel();
        String osName = System.getProperty("os.name");

        solarisWorkaround = (osName != null && (osName.indexOf("Solaris") != -1 || osName.indexOf("Irix") != -1));

        openButton = new JButton("Open");
        cancelButton = new JButton("Cancel");

        openButton.setDefaultCapable(true);

//    p.add(cancelButton);
//    p.add(openButton);

//    JPanel p2 = new JPanel();

        imageLabel = new JLabel("image: ");
        imageText = new JTextField(20);

//    p2.add(imageLabel);
//    p2.add(imageText);

        int diff = 0;
        if (solarisWorkaround)
            diff = 20;

        imageList = new JList(data);
        scrollPane = new JScrollPane(imageList);

        getContentPane().setLayout(null);
        scrollPane.setBounds(5, 5, 250, 155);
        imageLabel.setBounds(5, 165, 50 + diff, 20);
        imageText.setBounds(60, 165, 195, 20);
        cancelButton.setBounds(5, 190, 80 + diff, 30);
        openButton.setBounds(175, 190 - diff, 80 + diff, 30);

        getContentPane().add(scrollPane);
        getContentPane().add(imageLabel);
        getContentPane().add(imageText);
        getContentPane().add(cancelButton);
        getContentPane().add(openButton);

//    getContentPane().add(scrollPane);//, "West");//, "North");
//    getContentPane().add(p2);//, "South");//,"Center");
//    getContentPane().add(p);//, "East");//,"South");

        this.setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

        this.getRootPane().setDefaultButton(openButton);

        openButton.addActionListener(this);
        cancelButton.addActionListener(this);
        imageList.addMouseListener(this);
        imageList.addListSelectionListener(this);
        imageText.getDocument().addDocumentListener(this);

        this.setResizable(false);
        this.setTitle("Image Selection");

        this.setSize(265 + diff, 265 + ((diff == 0) ? diff : diff - 5));

    }

    public void setSelected(String image, boolean update) {
        this.selectedImage = image;
        if (update)
            imageText.setText(image);
    }

    public String getSelected() {
        return this.selectedImage;
    }

    private void close() {
        this.dispose();
    }

    /**
     * ****************************************************
     * ActionListener
     * *****************************************************
     */

    public void actionPerformed(ActionEvent e) {
        Object o = e.getSource();

        if (o == openButton)
            actionPerformed_OpenButton();
        else if (o == cancelButton)
            actionPerformed_CancelButton();

    }

    private void actionPerformed_OpenButton() {
//    System.out.println("selected :"+getSelected());
        processActionEvent(new ActionEvent(this, 0, getSelected()));
        close();
    }

    private void actionPerformed_CancelButton() {
        processActionEvent(new ActionEvent(this, 0, null));
        close();
    }

    /**
     * ****************************************************
     * ListSelectionListener
     * *****************************************************
     */

    public void valueChanged(ListSelectionEvent e) {
        setSelected((String) imageList.getSelectedValue(), true);
//    System.out.println((String)imageList.getSelectedValue());
    }

    /**
     * ****************************************************
     * MouseListener
     * *****************************************************
     */

    public void mouseClicked(MouseEvent e) {
        if (e.getClickCount() == 2) {
            int index = imageList.locationToIndex(e.getPoint());
            setSelected((String) imageList.getSelectedValue(), true);
            actionPerformed_OpenButton();
//      System.out.println("Double clicked on Item " + index);
        }
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    /**
     * ***********************************************
     * DocumentListener
     * ************************************************
     */

    public void changedUpdate(DocumentEvent e) {
        actionPerformed_imageText();
    }

    public void insertUpdate(DocumentEvent e) {
        actionPerformed_imageText();
    }

    public void removeUpdate(DocumentEvent e) {
        actionPerformed_imageText();
    }

    private void actionPerformed_imageText() {
//    imageList.clearSelection();
        setSelected(imageText.getText(), false);
    }

    /**
     * ***********************************************
     * Action event stuff
     * ************************************************
     */

    public synchronized void addActionListener(ActionListener l) {
        if (l == null)
            return;
        if (actionListeners == null)
            actionListeners = new Vector();

        actionListeners.add(l);
    }

    public synchronized void removeActionListener(ActionListener l) {
        if (l == null || actionListeners == null || actionListeners.size() <= 0)
            return;

        actionListeners.remove(l);

    }

    protected void processActionEvent(ActionEvent e) {
        if (actionListeners != null) {
            for (int i = 0; i < actionListeners.size(); i++)
                ((ActionListener) actionListeners.elementAt(i)).actionPerformed(e);
        }
    }

}


